﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class SelectRole : MonoBehaviour
{

    private GameObject _roleListContent;

    private Button _btnEnter;

    private ToggleGroup _roleListToggleGroup;


    private GameObject _modelStudio;
    private TouchRotate _modelTouchRotate;

    private int _selectRoleIndex = -1;
    private int _lastRoleIndex = 2;//记录上一次选中的角色（测试）
    private GameObject _modelPlace;//模型放置点

    private void Awake()
    {
        Debug.Log("SelectRole");

        _btnEnter = transform.Find("BtnEnter").GetComponent<Button>();
        _roleListContent = transform.Find("RoleList/Viewport/Content").gameObject;

        _roleListToggleGroup = _roleListContent.GetComponent<ToggleGroup>();
        _btnEnter.onClick.AddListener(onBtnEnterClick);

        _modelTouchRotate = gameObject.Find<TouchRotate>("TouchRotate");

        //_modelStudio = GameObject.Instantiate(Resources.Load<GameObject>("UI/SelectRole/ModelStudio"));
        _modelStudio = ResMgr.instance.GetInstance("UI/SelectRole/ModelStudio");
        _modelPlace = _modelStudio.Find<Transform>("ModelPlace").gameObject;

        //_modelTouchRotate.DragCallback = onTouchRotate;
        //初始化角色列表
        var i = 0;
        foreach (var roleInfo in UserData.instance.AllRole)
        {
            //foreach (var roleInfo in RoleTable.instance.GetAll())
            Debug.Log("" +roleInfo.Name);
            // 读取资源并实例化
            var roleItem = GameObject.Instantiate(Resources.Load<GameObject>("UI/SelectRole/RoleItem"));

            roleItem.transform.SetParent(_roleListContent.transform, false);
            //roleItem.transform.SetParent(transform, false);

            var textName = roleItem.transform.Find("Label").GetComponent<Text>();
            var toggle = roleItem.GetComponent<Toggle>();

            textName.text = roleInfo.Name;
            //textName.text = roleInfo.Value.Name;

            toggle.group = _roleListToggleGroup;
            var index = i;
            ++i;
            toggle.onValueChanged.AddListener((isOn) => {
                onToggleValueChanged(index, isOn);
            });
            toggle.isOn = index == _lastRoleIndex;

        }

    }

    private void onTouchRotate(PointerEventData eventData)
    {
        _modelPlace.transform.Rotate(new Vector3(0, -eventData.delta.x, 0));
    }

    private void onToggleValueChanged(int roleIndex, bool isOn)
    {

        Debug.Log(string.Format("{0},{1}", roleIndex, isOn));

        if (isOn)
        {
            if (_selectRoleIndex == roleIndex)
            {
                return;
            }
            _selectRoleIndex = roleIndex;

            //先清除之前的模型（清空ModelPlace下面的所有物体）
            _modelPlace.DestoryAllChildren();

            var curRoleInfo = UserData.instance.AllRole[roleIndex];

            var modelPath = RoleTable.instance[curRoleInfo.ModelID].ModelPath;
            Debug.Log("角色模型的路径" + modelPath);
            var model = ResMgr.instance.GetInstance(modelPath);


            model.transform.SetParent(_modelPlace.transform, false);


        }


    }

    private void onBtnEnterClick()
    {

        Debug.Log("选中了角色：" + _selectRoleIndex);
        // 发送一个选中角色消息
        SelectRoleCmd cmd = new SelectRoleCmd() { Index = _selectRoleIndex };
        Nets.instance.sendCmd(cmd);

    }
}
